//
//  BPCategoryViewController.m
//  BP_NewClient
//
//  Created by letv on 15/5/21.
//  Copyright (c) 2015年 normal. All rights reserved.
//  精品发型

#import "BPCategoryViewController.h"
#import "BPCategoryCollectionCell.h"
#import "AppDelegate.h"
#import "BPCategoryHeaderView.h"
#import "BPCategoryListViewController.h"
#import "BURequestManager.h"
#import "UIImageView+AFNetworking.h"

static NSString *key = @"identify";//重用标识
static NSString *key1 = @"header";
static NSString *key2 = @"footer";

@interface BPCategoryViewController (){

    NSArray *titleArray;
}

@end

@implementation BPCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"发型推荐";
    self.navigationItem.leftBarButtonItem = BACK_BAR_BUTTON_BACK(@selector(back:));
    [[AppDelegate shareApplication] hidesTabBar:YES animated:YES];

    titleArray = @[@"韩式长发",@"韩式短发",@"日式发型", @"男士发型",@"创意彩色",@"魅力中长发"];
   
    _myDataArray = [[NSMutableArray alloc] init];

    
    
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;

//    flowLayOut.headerReferenceSize = CGSizeMake(ScreenSize.width, 50);
    
    UICollectionView *collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - KNavBarHeight - 20) collectionViewLayout:flowLayOut];
    collectView.backgroundColor = [UIColor whiteColor];
    collectView.delegate = self;
    collectView.dataSource = self;
    [self.view addSubview:collectView];
    
    [collectView registerNib:[UINib nibWithNibName:@"BPCategoryCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"BPCategoryCollectionCell"];
    
    // 请求数据
    [[BURequestManager sharedRequest] requestGetWithParameterWithBlockDic:FAXING_CATEGORY SetParamer:nil success:^(NSDictionary *dic){
        _myDataArray = [dic objectForKey:@"data"];
        [collectView reloadData];
    } failure:^(NSString *errorMsg){
        [self showNoticeMsg:errorMsg];
    }];
}


#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _myDataArray.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"BPCategoryCollectionCell";
    BPCategoryCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
//    cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];
//    cell.backgroundColor = [UIColor lightTextColor];
    
    
    NSDictionary *dic = OBJECT_OF_ATINDEX(_myDataArray, indexPath.row);
    
    NSString *url = [dic objectForKey:@"faxing_img"];
    [cell.imgView setImageWithURL1:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@""]];
    cell.label.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"faxing_name"]];
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    collectionView.backgroundColor = [UIColor clearColor];
    return CGSizeMake(ScreenWidth/3 -15, 120);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 5, 10, 10
                            );
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.backgroundColor = [UIColor whiteColor];
    
    NSDictionary *dic = [_myDataArray objectAtIndex:indexPath.row];
    BPCategoryListViewController *list = [[BPCategoryListViewController alloc] init];
    list.category_id = [dic objectForKey:@"faxing_category"];
    [self.navigationController pushViewController:list animated:YES];
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    attributes.size = CGSizeMake(215/3.0, 303/3.0);
    attributes.center=CGPointMake(80*(indexPath.item+1), 62.5+125*indexPath.section);
    return attributes;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
